//
//  ViewController.swift
//  RouterPushProtocol
//
//  Created by Toj on 2/21/22.
//

import UIKit
import MediaPlayer

class ViewController: UIViewController {

    private var timer: Timer?
    
    private lazy var label: UILabel = {
        let view = UILabel()
        view.text = "0"
        view.textColor = .red
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        view.addSubview(label)
        label.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        
        if #available(iOS 11.0, *) {
            NotificationCenter.default.addObserver(self, selector: #selector(capturedDidChange(sender:)), name: UIScreen.capturedDidChangeNotification, object: nil)
        } else {
            // Fallback on earlier versions
        }
        
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countly), userInfo: nil, repeats: true)
        
        if #available(iOS 11.0, *) {
            print(UIScreen.main.isCaptured)
        } else {
            // Fallback on earlier versions
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(pmVolumeViewWirelessRouteActiveDidChange(sender:)), name: NSNotification.Name.MPVolumeViewWirelessRouteActiveDidChange, object: nil)
        
        
    }
    var count = 0
    @objc private func countly() {
        count += 1
        label.text = String(count)
    }
    
    @objc private func capturedDidChange(sender: Notification) {
        // print("11111")
        // print(sender.description)
        // print(sender.userInfo)
        // print(sender.object)
        
        // let alert = UIAlertView(title: nil, message: "1111[安全提醒]内含个人资金账户。不要截图，录制或分享给他人以保障资金账户安全。", delegate: nil, cancelButtonTitle: "确定")
        // alert.show()
        //
        // if #available(iOS 11.0, *) {
        // print(UIScreen.main.isCaptured)
        // } else {
        // Fallback on earlier versions
        // }
    }
    
    @objc private func pmVolumeViewWirelessRouteActiveDidChange(sender: Notification) {
        
        // print("22222")
        // print(sender.description)
        // print(sender.userInfo)
        // print(sender.object)
        
        // let alert = UIAlertView(title: nil, message: "222222[安全提醒]内含个人资金账户。不要截图，录制或分享给他人以保障资金账户安全。", delegate: nil, cancelButtonTitle: "确定")
        // alert.show()
        
        //  if #available(iOS 11.0, *) {
        // print(UIScreen.main.isCaptured)
        // } else {
        // Fallback on earlier versions
        // }
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}
